package com.bigevent.bigevent.service.impl;

import com.bigevent.bigevent.entity.Article;
import com.bigevent.bigevent.entity.PageBean;
import com.bigevent.bigevent.mapper.ArticleMapper;
import com.bigevent.bigevent.service.ArticleService;
import com.bigevent.bigevent.utils.AliOssUtil;
import com.bigevent.bigevent.utils.UserUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author yangxudong
 * @description:
 * @date 2024/2/10 11:26
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articalMapper;

    @Override
    public void registArticle(Article article) {
        article.setCreateUser(UserUtil.getUserId());
        articalMapper.registArticle(article);
    }

    @Override
    public PageBean<Article> getArticleList(Integer pageNum, Integer pageSize, Integer categoryId, String state) {
        // 创建pageBean对象
        PageBean<Article> pageBean = new PageBean();
        // PageHelper开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        // 调用mapper查询数据
        List<Article> articles = articalMapper.getArticleList(UserUtil.getUserId(), categoryId, state);
        // 结果强转
        Page<Article> page = (Page<Article>) articles;
        // 封装pageBean
        pageBean.setItems(page.getResult());
        pageBean.setTotal(page.getTotal());
        // 返回pageBean
        return pageBean;
    }

    @Override
    public String uploadArticleImage(MultipartFile file) {
        try {
            return AliOssUtil.uploadFile(file.getOriginalFilename(), file.getInputStream());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updateArticle(Article article) {
        article.setCreateUser(UserUtil.getUserId());
        articalMapper.updateArticle(article);
    }

    @Override
    public Article getArticleDetail(Integer id) {
        return articalMapper.getArticleDetail(id);
    }

    @Override
    public void deleteArticle(Integer id) {
        articalMapper.deleteArticle(id);
    }
}
